using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;

namespace Widgetsphere.WebUtils
{
	public static class Extensions
	{
		public static void AppendCData(this XmlTextWriter writer, string tag, string data)
		{
			writer.WriteStartElement(tag);
			writer.WriteCData(data);
			writer.WriteEndElement();
		}

		public static string Truncate(this string s, int maxLength)
		{
			return s.Truncate(maxLength, false);
		}

		/// <summary>
		/// Truncates a string to a maximum length
		/// </summary>
		/// <param name="s">The string to truncate</param>
		/// <param name="maxLength">The maximum length it should be</param>
		/// <returns></returns>
		public static string Truncate(this string s, int maxLength, bool addEllipsis)
		{
			if (string.IsNullOrEmpty(s)) return s;
			if (s.Length > maxLength)
			{
				return s.Substring(0, maxLength) + (addEllipsis ? "..." : "");
			}
			else
			{
				return s;
			}
		}
	}

}